package com.general.interceptor.jdk;

import com.general.interceptor.ChainableInvocationContext;
import com.general.interceptor.ReflectiveMethodInvocationContext;

import javax.interceptor.InvocationContext;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @description: InvocationHandlerAdapter
 * @author: general
 * @version: 1.0
 * @create: 2021-10-26 20:17
 **/
public class InvocationHandlerAdapter implements InvocationHandler {

    private Object[] interceptors;

    private Object source;

    public InvocationHandlerAdapter(Object[] interceptors) {
        this.interceptors = interceptors;
    }

    public InvocationHandlerAdapter(Object source, Object[] interceptors) {
        this.source = source;
        this.interceptors = interceptors;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        InvocationContext invocationContext = new ReflectiveMethodInvocationContext(proxy, method, args);

        ChainableInvocationContext chainableContext =
                new ChainableInvocationContext(invocationContext, interceptors );
        return chainableContext.proceed();
    }
}
